package es.unex.meigas.gui;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import es.unex.meigas.core.DasocraticElement;

public abstract class DasocraticElementPanel extends DasocraticInfoPanel {

	private JTabbedPane jTabbedPane;
	protected JPanel[] m_Panels;

	public DasocraticElementPanel(DasocraticElement element, MeigasPanel meigasPanel){

		super(element, meigasPanel);

	}

	public boolean checkDataAndUpdate(){

		Object panel;
		boolean bReturn = true;
		int i;

		for (i = 0; i < m_Panels.length; i++){
			panel = m_Panels[i];
			if (panel instanceof DasocraticInfoPanel){
				bReturn = bReturn && ((DasocraticInfoPanel)panel).checkDataAndUpdate();
			}
		}

		return bReturn;

	}

	protected void initGUI() {

		int i;
		JPanel panel;

		try {
			{
				BorderLayout layout = new BorderLayout();
				this.setLayout(layout);
				this.setPreferredSize(new java.awt.Dimension(500, 300));
				jTabbedPane = new JTabbedPane();
				for (i = 0; i < m_Panels.length; i++){
					panel = (JPanel) m_Panels[i];
					jTabbedPane.addTab(panel.getName(), panel);
				}
				this.add(jTabbedPane);
				jTabbedPane.addChangeListener(new ChangeListener() {
					public void stateChanged(ChangeEvent evt) {
						updatePanels();
					}
				});

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void updatePanels() {

		int i;

		checkDataAndUpdate();

		for (i = 0; i < m_Panels.length; i++){
			JPanel panel = m_Panels[i];
			if (panel instanceof DasocraticInfoPanel){
				((DasocraticInfoPanel)panel).updateContent();
			}
		}

	}

}
